package com.dkd.manage.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelDto;
import com.dkd.manage.domain.po.Channel;
import com.dkd.manage.domain.po.VendingMachine;
import com.dkd.manage.domain.vo.ChannelVo;
import com.dkd.manage.mapper.VendingMachineMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.service.IChannelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售货机货道Service业务层处理
 * 
 * @author ming
 * @date 2024-10-26
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChannelServiceImpl implements IChannelService 
{
    private final ChannelMapper channelMapper;

    private final VendingMachineMapper vendingMachineMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<ChannelVo> selectChannelVoList(Channel channel)
    {
        return channelMapper.selectChannelVoList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    @Override
    @Transactional
    public int updateBatchConfig(ChannelDto channelDto) {
        if (ObjectUtil.isEmpty(channelDto) && CollUtil.isEmpty(channelDto.getChannelList())) {
            return 0;
        }
        channelDto.getChannelList().forEach(channel -> {
            Channel info = channelMapper.selectChannelInfo(channel.getInnerCode(), channel.getChannelCode());
            info.setSkuId(channel.getSkuId());
            info.setUpdateTime(DateUtils.getNowDate());
            channelMapper.updateBatchConfig(info);
        });
        return 1;
    }


    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }
}
